#pragma once
// mysql版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "mysql_include/mysql.h"

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    // 定义一个question结构体用来存放question的基本信息
    struct Question
    {
        std::string number; // 编号
        std::string title;  // 标题
        std::string star;   // 难度星级
        int cpu_limit;      // cpu限制时间
        int mem_limit;      // 内存限制
        std::string desc;   // 描述
        std::string header; // 给用户展示的代码
        std::string tail;   // test
    };

    const std::string questions = "questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "918831";
    const std::string db = "oj";
    const int port = 3306;

    class Model
    {
    public:
        Model()
        {
        }
        ~Model()
        {
        }
        // 运行sql语句，将结果存在out
        bool QueryMysql(const std::string &sql, std::vector<Question> *out)
        {
            // 创建mysql对象
            MYSQL *my = mysql_init(nullptr);

            // 连接mysql
            if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                Log(FATAL) << "数据库连接失败!"
                           << "\n";
                return false;
            }

            // 设置mysql编码格式
            mysql_set_character_set(my, "utf8");

            // 从mysql中读取数据
            if (0 != mysql_query(my, sql.c_str()))
            {
                Log(WARNING) << sql << "execute error!"
                             << "\n";
                return false;
            }

            // 提取结果
            MYSQL_RES *res = mysql_store_result(my);

            // 分析结果,拿出读取出的行数和列数
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);

            Question q;

            // 将数据存在out中
            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res); // 处理每一行中的各个数据
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);

                out->push_back(q);
            }

            free(res);
            mysql_close(my);
            return true;
        }

        // 拿走所有题目
        bool GetAllQuestions(std::vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += questions;
            return QueryMysql(sql, out);
        }
        // 拿到一道题
        bool GetOneQuestion(const std::string number, Question *q)
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += questions;
            sql += " where id=";
            sql += number;
            std::vector<Question> tmp;
            if(QueryMysql(sql, &tmp))
            {
                if(tmp.size() == 1)
                {
                    *q=tmp[0];
                    res=true;
                }
            }
            return res;
        }
    };
}